var igd = new Object();
igd.dom = new Object();
igd.dom.getbyid = function(id){
	return document.getElementById(id);
}
igd.dom.getbyname = function(name){
	return document.getElementByName(name);
}


igd.event = function(obj,callback){
	igd.event.prototype.type = "EVENT";
	this.obj = obj;
	this.callback = callback;
	//绑定事件到对象
	igd.event.prototype.bind = function(){
	}
	//取消绑定
	igd.event.prototype.unbind = function(){
	}
}

var userAgent = navigator.userAgent.toLowerCase();
igd.browser =  {
	version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
	safari: /webkit/.test(userAgent),
	opera: /opera/.test(userAgent),
	msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
	mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
}

igd.base = new Object();
//队列
igd.base.queue = function(name){
	igd.base.queue.prototype.type = "QUEUE";
	this.name = name;
	this.queue = new Array();
	//入队列
	igd.base.queue.prototype.enqueue = function(obj){
		if(this.indexof(obj) >= 0){
			alert("obj is already in queue");
		}
		else{
			this.queue.push(obj);
		}
	}
	//出队列
	igd.base.queue.prototype.dequeue = function(){
		return this.queue.shift();
	}
	//队列长度
	igd.base.queue.prototype.length = function(){
		return this.queue.length;
	}
	//根据索引删除
	igd.base.queue.prototype.deletebyindex = function(index){
		this.queue.splice(index,1);
	}
	//根据对象删除
	igd.base.queue.prototype.deletebyobject = function(obj){
		this.queue.splice(this.indexof(obj),1);
	}
	//取index处的数据
	igd.base.queue.prototype.index = function(i){
		if(i<=0 && i > this.queue.length){
			alert("invalid index");
			return null;
		}
		return this.queue[i];
	}
	//返回一个对象在队列中的index值
	igd.base.queue.prototype.indexof = function(e){
		for(var i = 0; i < this.queue.length; i++){
			if(this.queue[i] == e){
				return i;
			}
		}
		//alert("no such element");
		return -1;
	}
}
igd.base.each = function(obj,fn,args) {
	if (args){
		if (obj.length == undefined )
			for(var i in obj)
				fn.apply(obj[i], args);
			else
				for(var i = 0, ol = obj.length; i < ol; i++)
					if ( fn.apply( obj[i], args ) === false) break;
	} else {
		if ( obj.length == undefined )
		for ( var i in obj )
		fn.call( obj[i], i, obj[i] );
		else
		for ( var i = 0, ol = obj.length, val = obj[0];
		i < ol && fn.call(val,i,val) !== false; val = obj[++i] ){}
	}

	return obj;
}

//消息框
igd.msgbox = function(id,msg,timeout){
	igd.msgbox.prototype.type = "MSGBOX";
	this.id = id;
	this.msg = msg;
	this.state = "INIT";
	this.timeout = timeout;
	igd.msgbox.prototype.show = function(){
		var el = igd.dom.getbyid(this.id);
		if(el && this.state == "INIT"){
			el.innerHTML = '<div class="msgbox">'+
									'<div class="msgbox_t"></div>' +
									'<div class="msgbox_p1"></div>' +
									'<div class="msgbox_p2"></div>' +
									'<div class="msgbox_box">'+this.msg+'</div>' +
									'<div class="msgbox_p2"></div>' +
									'<div class="msgbox_p1"></div>' +
									'<div class="msgbox_b"></div>' +
									'</div>';
			this.state = "SHOW";
			timer_queue.enqueue(new igd.timer(this.timeout, this, function(data){data.hide();msgbox_queue.deletebyobject(data)}));
		}
	}
	igd.msgbox.prototype.hide = function(){
		var el = igd.dom.getbyid(this.id);
		if(el && this.state == "SHOW"){
			el.innerHTML="";
			this.state = "HIDE";
		}
	}
	igd.msgbox.prototype.setTimeout = function(timeout){
		this.timeout = timeout;
	}
};
//定时器
igd.timer = function(timeout, data, callback){
	igd.timer.prototype.type = "TIMER"
	this.callback = callback;
	this.timeout =  jiffies + timeout;
	this.data = data;
	igd.timer.prototype.run = function(){
		return this.callback(this.data);
	}
}

//igd.ajax
igd.ajax = new Object();
igd.ajax.prototype.READY_STATE_UNINITIALIZED = 0;
igd.ajax.prototype.READY_STATE_LOADING = 1;
igd.ajax.prototype.READY_STATE_LOADED = 2;
igd.ajax.prototype.READY_STATE_INTERACTIVE = 3;
igd.ajax.prototype.READY_STATE_COMPLETE = 4;
igd.ajax.param = function(a) {
	var s = [];

	if (a.constructor == Array){
		igd.base.each(a, function(){
			s.push(encodeURIComponent(this.name) + "=" + encodeURIComponent(this.value));
		});
	}
	else{
		for (var j in a){
			if (a[j] && a[j].constructor == Array){
				igd.base.each(a[j], function(){
					s.push(encodeURIComponent(j) + "=" + encodeURIComponent(this));
				});
			}
			else{
				s.push(encodeURIComponent(j) + "=" + encodeURIComponent(a[j]));
			}
		}
	}

	return s.join("&").replace(/%20/g, "+");
}
igd.ajax.request = function(type,url,params,callback){
	igd.ajax.request.prototype.type = "REQUEST";
	var handle = null;
	if(window.XMLHttpRequest){
		handle = new XMLHttpRequest();
	}
	else if(typeof ActiveXObject != "undefined"){
		handle = new ActiveXObject("Microsoft.XMLHTTP");
	}
	this.handle = handle;
	if(type == "GET"){
		this.type = "GET";
	}
	else{
		this.type = "POST";
	}
	this.state = "INIT";
	this.url = url;
	this.params = params;
	this.callback = callback;
	igd.ajax.request.prototype.send = function(){
		if(this.handle){
			var _this = this;
			this.handle.onreadystatechange = function(){
				_this.state = "END";
				if(_this.handle.readyState == igd.ajax.READY_STATE_UNINITIALIZED){
				}
				else if(_this.handle.readyState == igd.ajax.READY_STATE_LOADING){
				}
				else if(_this.handle.readyState == igd.ajax.READY_STATE_LOADED){
				}
				else if(_this.handle.readyState == igd.ajax.READY_STATE_INTERACTIVE){
				}
				else if (_this.handle.readyState == igd.ajax.READY_STATE_COMPLETE){
					var data = _this.handle.responseText;
					var resp = new igd.ajax.response(_this,data);
					ajax_resp_queue.enqueue(resp);
					ajax_wait_queue.deletebyobject(_this);
				}
				else{
					data = "error";
				}
			};
			this.handle.open(this.type,url,true);
			this.handle.setRequestHeader(
				"Content-Type",
				"application/x-www-form-urlencoded");
			try{
				this.handle.send(igd.ajax.param(this.params));
			}
			catch(e){
				alert(e);
			}
			this.state = "SEND";
		}
	}
	igd.ajax.request.prototype.cancel = function(){
		this.handle.abort();
	}
}
igd.ajax.response = function(request,data){
	igd.ajax.response.prototype.type = "RESPONSE";
	this.request = request;
	this.data = data;
	igd.ajax.response.prototype.callback = function(){
		this.request.callback(this.data);
	}
}
igd.ajax.post = function(url,params,callback){
	igd.ajax.response.prototype.type = "POST";
	this.request = new igd.ajax.request("POST",url,params,callback);
	ajax_requ_queue.enqueue(this.request);
	igd.ajax.post.prototype.cancel = function(){
		this.request.cancel();
	}
}
igd.ajax.get = function(url,params,callback){
	igd.ajax.response.prototype.type = "GET";
	this.request = new igd.ajax.request("GET",url,params,callback);
	ajax_requ_queue.enqueue(this.request);
	igd.ajax.get.prototype.cancel = function(){
		this.request.cancel();
	}
}
igd.ajax.load = function(url,params,callback){
	igd.ajax.response.prototype.type = "LOAD";
	this.request = new request("POST",url,params,callback);
	ajax_requ_queue.enqueue(this.request);
}

igd.task = function(data,callback){
	igd.task.prototype.type = "TASK";
	this.data = data;
	this.callback= callback;
	igd.task.prototype.run = function(){
		this.callback(this.data);
	}
}

var HZ = 1000;
var ticks = 100;
var jiffies = 0;
var lock = 0;
var cgi_base_path = "/router/";

var msgbox_queue = new igd.base.queue("msg queue");
var timer_queue = new igd.base.queue("timer queue");
var ajax_requ_queue = new igd.base.queue("ajax request queue");
var ajax_wait_queue = new igd.base.queue("ajax wait response queue");
var ajax_resp_queue = new igd.base.queue("ajax response queue");
var event_queue = new igd.base.queue("event queue");
var taskqueue = new igd.base.queue("task queue");

//运行定时器队列
function timer_queue_run(){
	var len = timer_queue.length();
	for(var i = 0; i < len; i ++){
		//从队列取出
		var t = timer_queue.dequeue();
		//判断是否超时
		if(jiffies >= t.timeout){
			//生成task，放入task队列等待执行
			taskqueue.enqueue(new igd.task(t,function(data){data.run();}));
		}
		else{
			//重新放回队列，等待以后执行
			timer_queue.enqueue(t);
		}
	}
}

//运信消息框队列
function msgbox_queue_run(){
	var len = msgbox_queue.length();
	if(len > 0){
		var box = msgbox_queue.index(0);
		if(box.state == "INIT"){
			if(len > 1){
				box.setTimeout(100);
			}
			taskqueue.enqueue(new igd.task(box,function(data){data.show()}))
		}
	}
}

//运信ajax请求队列
function ajax_queue_run(){
	var len = ajax_requ_queue.length();
	for(var i = 0; i < len; i++){
		var req = ajax_requ_queue.dequeue();
		taskqueue.enqueue(new igd.task(req,function(data){data.send()}));
	}
	len = ajax_wait_queue.length();
	for(var i = 0; i < len; i ++){
	}
	len = ajax_resp_queue.length();
	for(var i = 0; i < len; i ++){
		taskqueue.enqueue(new igd.task(ajax_resp_queue.dequeue(),function(data){data.callback()}))
	}
}

function event_queue_run(){
	var len = event_queue.length();
	for(var i = 0; i < len; i ++){
		var e = event_queue.dequeue();
		taskqueue.enqueue(new igd.task(e,function(data){data.callback()}));
	}
}

function task_queue_run(){
	var len = taskqueue.length()
	for(var i = 0; i < len; i ++){
		taskqueue.dequeue().run();
	}
}

function taskrun(){
	//update jiffies
	jiffies = jiffies + ticks;

	if(lock){
		return;
	}
	lock = 1;

	//run timer queue
	timer_queue_run();

	//run msg queue
	msgbox_queue_run();

	//run ajax queue
	ajax_queue_run();

	//run dom event queue
	event_queue_run();

	//run real task
	task_queue_run();

	lock = 0;
}
window.setInterval(taskrun, ticks);